package com.lhoverc.controller.user;

import com.lhoverc.annotation.Log;
import com.lhoverc.pojo.Result;
import com.lhoverc.pojo.VO.UserInfoVO;
import com.lhoverc.pojo.dto.LoginUserDto;
import com.lhoverc.pojo.dto.RegisterUserDto;
import com.lhoverc.pojo.entity.UserEntity;
import com.lhoverc.service.user.UserService;
import com.lhoverc.utils.JwtUtil;
import com.lhoverc.utils.MailMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

import static com.lhoverc.constant.ErrorConstant.LOGIN_ERROR;
import static com.lhoverc.constant.ErrorConstant.RECAPTCHA_ERROR;

/**
 * @Author: LiCheng
 * @CreateTime: 2025-04-12
 * @Description:
 * @Version: 1.0
 */

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MailMsg mailMsg;

    @GetMapping("getUserInfo/{id}")
    public Result getUserInfo(@PathVariable Integer id) {
        UserInfoVO userInfoVo = userService.getUserInfo(id);
        return Result.success(userInfoVo);
    }

    /**
     * 用户登录
     *
     * @param loginUserDto
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginUserDto loginUserDto) {
        log.info("用户登录：{}", loginUserDto);
        UserEntity user = userService.login(loginUserDto);

        //登录成功，生成令牌，下发令牌
        if (user != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", user.getId());
            claims.put("username", user.getUsername());
            claims.put("image", user.getImage());
            claims.put("status", user.getStatus());

            String jwt = JwtUtil.generateJwt(claims); //jwt包含了当前登录的员工信息
            return Result.success(jwt);
        }
        //登录失败
        return Result.error(LOGIN_ERROR);
    }

//    /**
//     * 根据id删除用户
//     *
//     * @param id
//     * @return
//     */
//    @Log
//    @DeleteMapping("/{id}")
//    public Result deleteUser(@PathVariable Integer id) {
//        log.info("删除用户的id：{}", id);
//        userService.deleteUser(id);
//        return Result.success();
//    }

    /**
     * 注册新用户
     *
     * @param registerUserDto
     * @return
     */
    @PostMapping("/register")
    public Result addUser(@RequestBody RegisterUserDto registerUserDto) {
        log.info("添加新用户：{}", registerUserDto);
        userService.addUser(registerUserDto);
        return Result.success();
    }

    @GetMapping("/forgetPassword")
    public Result forgetPassword(String email, String reCaptcha, String password) {
        String value = mailMsg.getValue(email);
        log.info("value: {}", value);
        if (value == null){
            return Result.error(RECAPTCHA_ERROR);
        } else if (!value.equals(reCaptcha)) {
            return Result.error(RECAPTCHA_ERROR);
        }
        userService.updatePassword(email, password);
        return Result.success();
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Log
    @PostMapping("/update")
    public Result updateUser(@RequestBody UserEntity user) {
        log.info("修改用户信息：{}", user);
        userService.updateUser(user);
        return Result.success();
    }
}
